<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.png?v=5.1.4">


  <link rel="mask-icon" href="/images/favicon.png?v=5.1.4" color="#222">





  <meta name="keywords" content="Linux,教程,vim," />





  <link rel="alternate" href="/atom.xml" title="平步青云win" type="application/atom+xml" />






<meta name="description" content="本文介绍linux中的编辑器vim操作,以及基本命令操作">
<meta name="keywords" content="Linux,教程,vim">
<meta property="og:type" content="article">
<meta property="og:title" content="Vim教程">
<meta property="og:url" content="https://zxpgo.github.io/2018/08/13/Vim教程/index.html">
<meta property="og:site_name" content="平步青云win">
<meta property="og:description" content="本文介绍linux中的编辑器vim操作,以及基本命令操作">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-09-20T07:58:44.831Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vim教程">
<meta name="twitter:description" content="本文介绍linux中的编辑器vim操作,以及基本命令操作">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":10,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'PAO8LM7QB1',
      apiKey: '',
      indexName: 'Blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zxpgo.github.io/2018/08/13/Vim教程/"/>





  <title>Vim教程 | 平步青云win</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7a4517a3ce6d7c50203655d056f01ac3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">平步青云win</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-随笔">
          <a href="/sui" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            随笔
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
			
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zxpgo.github.io/2018/08/13/Vim教程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zxp">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="平步青云win">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Vim教程</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-13T17:24:24+08:00">
                2018-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/实验楼学习笔记之Linux/" itemprop="url" rel="index">
                    <span itemprop="name">实验楼学习笔记之Linux</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/13/Vim教程/" class="leancloud_visitors" data-flag-title="Vim教程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <p>本文介绍linux中的编辑器vim操作,以及基本命令操作<a id="more"></a></p>
<h1 id="Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式："><a href="#Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式：" class="headerlink" title="Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式："></a>Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式：</h1><ul>
<li>普通模式(Normal mode)</li>
</ul>
<p>在普通模式中，用的编辑器命令，比如移动光标，删除文本等等。这也是Vim启动后的默认模式。这正好和许多新用户期待的操作方式相反（大多数编辑器默认模式为插入模式）。</p>
<p>Vim强大的编辑能来自于其普通模式命令。普通模式命令往往需要一个操作符结尾。例如普通模式命令dd删除当前行，但是第一个”d”的后面可以跟另外的移动命令来代替第二个d，比如用移动到下一行的”j”键就可以删除当前行和下一行。另外还可以指定命令重复次数，2dd（重复dd两次），和dj的效果是一样的。用户学习了各种各样的文本间移动／跳转的命令和其他的普通模式的编辑命令，并且能够灵活组合使用的话，能够比那些没有模式的编辑器更加高效地进行文本编辑。</p>
<p>在普通模式中，有很多方法可以进入插入模式。比较普通的方式是按a（append／追加）键或者i（insert／插入）键。</p>
<ul>
<li>插入模式(Insert mode)</li>
</ul>
<p>在这个模式中，大多数按键都会向文本缓冲中插入文本。大多数新用户希望文本编辑器编辑过程中一直保持这个模式。</p>
<p>在插入模式中，可以按ESC键回到普通模式。</p>
<p>可视模式(Visual mode)<br>这个模式与普通模式比较相似。但是移动命令会扩大高亮的文本区域。高亮区域可以是字符、行或者是一块文本。当执行一个非移动命令时，命令会被执行到这块高亮的区域上。Vim的”文本对象”也能和移动命令一样用在这个模式中。</p>
<ul>
<li>选择模式(Select mode)<br>这个模式和无模式编辑器的行为比较相似（Windows标准文本控件的方式）。这个模式中，可以用鼠标或者光标键高亮选择文本，不过输入任何字符的话，Vim会用这个字符替换选择的高亮文本块，并且自动进入插入模式。</li>
</ul>
<ul>
<li>命令行模式(Command line mode)<br>在命令行模式中可以输入会被解释成并执行的文本。例如执行命令（:键），搜索（/和?键）或者过滤命令（!键）。在命令执行之后，Vim返回到命令行模式之前的模式，通常是普通模式。</li>
</ul>
<ul>
<li>Ex模式(Ex mode)<br>这和命令行模式比较相似，在使用:visual命令离开Ex模式前，可以一次执行多条命令。</li>
</ul>
<p>这其中我们常用到就是普通模式、插入模式和命令</p>
<pre><code>vim  文件名 //如果文件名不存在，会自动创建文件并打开
vim nginx.conf //进入普通模式
a 或 i  //进入插入模式
Esc  //退出插入模式，进入普通模式
： //退出普通模式，进入命令行模式
命令行模式下： wq保存退出
</code></pre><h1 id="游标移动"><a href="#游标移动" class="headerlink" title="游标移动"></a>游标移动</h1><p>按Esc进入普通模式，在该模式下使用方向键或者h,j,k,l键可以移动游标。</p>
<table>
<thead>
<tr>
<th>按键</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>h</td>
<td>左</td>
</tr>
<tr>
<td>l</td>
<td>右（小写L）</td>
</tr>
<tr>
<td>j</td>
<td>下</td>
</tr>
<tr>
<td>k</td>
<td>上</td>
</tr>
<tr>
<td>w</td>
<td>移动到下一个单词</td>
</tr>
<tr>
<td>b</td>
<td>移动到上一个单词</td>
</tr>
</tbody>
</table>
<h1 id="从普通模式进入插入模式"><a href="#从普通模式进入插入模式" class="headerlink" title="从普通模式进入插入模式"></a>从普通模式进入插入模式</h1><p>按Esc进入普通模式，在该模式下使用方向键或者h,j,k,l键可以移动游标。</p>
<table>
<thead>
<tr>
<th>按键</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>h</td>
<td>左</td>
</tr>
<tr>
<td>l</td>
<td>右（小写L）</td>
</tr>
<tr>
<td>j</td>
<td>下</td>
</tr>
<tr>
<td>k</td>
<td>上</td>
</tr>
<tr>
<td>w</td>
<td>移动到下一个单词</td>
</tr>
<tr>
<td>b</td>
<td>移动到上一个单词</td>
</tr>
</tbody>
</table>
<h1 id="保存文件"><a href="#保存文件" class="headerlink" title="保存文件"></a>保存文件</h1><pre><code>:w 文件名 //可以将文档另存为其他文件名或存到其它路径下

:w //保存文件
:w test //另存为test文件
</code></pre><h1 id="退出vim"><a href="#退出vim" class="headerlink" title="退出vim"></a>退出vim</h1><p>命令行模式下退出vim<br>从普通模式输入:进入命令行模式，输入wq回车，保存并退出编辑</p>
<p>以下为其它几种退出方式：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>:q!</td>
<td>强制退出，不保存</td>
</tr>
<tr>
<td>:q</td>
<td>退出</td>
</tr>
<tr>
<td>:wq!</td>
<td>强制保存并退出</td>
</tr>
<tr>
<td>:w &lt;文件路径&gt;</td>
<td>另存为</td>
</tr>
<tr>
<td>:saveas 文件路径</td>
<td>另存为</td>
</tr>
<tr>
<td>:x</td>
<td>保存并退出</td>
</tr>
<tr>
<td>:wq</td>
<td>保存并退出</td>
</tr>
</tbody>
</table>
<p>普通模式下退出vim<br>普通模式下输入Shift+zz即可保存退出vim</p>
<h1 id="普通模式下删除vim文本信息"><a href="#普通模式下删除vim文本信息" class="headerlink" title="普通模式下删除vim文本信息"></a>普通模式下删除vim文本信息</h1><p>进入普通模式，使用下列命令可以进行文本快速删除：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>x</td>
<td>删除游标所在的字符</td>
</tr>
<tr>
<td>X</td>
<td>删除游标所在前一个字符</td>
</tr>
<tr>
<td>Delete</td>
<td>同x</td>
</tr>
<tr>
<td>dd</td>
<td>删除整行</td>
</tr>
<tr>
<td>dw</td>
<td>删除一个单词（不适用中文）</td>
</tr>
<tr>
<td>d$或D</td>
<td>删除至行尾</td>
</tr>
<tr>
<td>d^</td>
<td>删除至行首</td>
</tr>
<tr>
<td>dG</td>
<td>删除到文档结尾处</td>
</tr>
<tr>
<td>d1G</td>
<td>删至文档首部</td>
</tr>
</tbody>
</table>
<h1 id="vim重复命令（普通模式下）"><a href="#vim重复命令（普通模式下）" class="headerlink" title="vim重复命令（普通模式下）"></a>vim重复命令（普通模式下）</h1><pre><code>. 表示重复执行上次命令  //向输入x删除第一个字符，再输入.接着删除一个字符
10x 表示删除10连续个字符
3dd 表示删除3行文本
dw或daw 删除一个单词
dNw 删除N个单词
</code></pre><p>#游标快速跳转</p>
<h2 id="行间跳转"><a href="#行间跳转" class="headerlink" title="行间跳转."></a>行间跳转.</h2><table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>nG(n Shift+g)</td>
<td>游标移动到第 n 行(如果默认没有显示行号，请先进入命令模式，输入:set nu以显示行号)</td>
</tr>
<tr>
<td>gg</td>
<td>游标移动到到第一行</td>
</tr>
<tr>
<td>G(Shift+g)</td>
<td>到最后一行</td>
</tr>
</tbody>
</table>
<h2 id="行内跳转"><a href="#行内跳转" class="headerlink" title="行内跳转"></a>行内跳转</h2><table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>w</td>
<td>到下一个单词的开头</td>
</tr>
<tr>
<td>e</td>
<td>到当前单词的结尾</td>
</tr>
<tr>
<td>b</td>
<td>到前一个单词的开头</td>
</tr>
<tr>
<td>ge</td>
<td>到前一个单词的结尾</td>
</tr>
<tr>
<td>0或^</td>
<td>到行头</td>
</tr>
<tr>
<td>$</td>
<td>到行尾</td>
</tr>
<tr>
<td>f&lt;字母&gt;</td>
<td>向后搜索&lt;字母&gt;并跳转到第一个匹配的位置(非常实用)</td>
</tr>
<tr>
<td>F&lt;字母&gt;</td>
<td>向前搜索&lt;字母&gt;并跳转到第一个匹配的位置</td>
</tr>
<tr>
<td>t&lt;字母&gt;</td>
<td>向后搜索&lt;字母&gt;并跳转到第一个匹配位置之前的一个字母(不常用)</td>
</tr>
<tr>
<td>T&lt;字母&gt;</td>
<td>向前搜索&lt;字母&gt;并跳转到第一个匹配位置之后的一个字母(不常用)</td>
</tr>
</tbody>
</table>
<h1 id="复制粘贴和剪切"><a href="#复制粘贴和剪切" class="headerlink" title="复制粘贴和剪切"></a>复制粘贴和剪切</h1><h2 id="复制及粘贴"><a href="#复制及粘贴" class="headerlink" title="复制及粘贴"></a>复制及粘贴</h2><p>普通模式中使用y复制</p>
<p>普通模式中，yy复制游标所在的整行（3yy表示复制3行）</p>
<p>普通模式中，y^ 复制至行首，或y0。不含光标所在处字符。</p>
<p>普通模式中，y$ 复制至行尾。含光标所在处字符。</p>
<p>普通模式中，yw 复制一个单词。</p>
<p>普通模式中，y2w 复制两个单词。</p>
<p>普通模式中，yG 复制至文本末。</p>
<p>普通模式中，y1G 复制至文本开头。</p>
<p>普通模式中使用 p 粘贴</p>
<p>普通模式中，p(小写)代表粘贴至光标后（下）<br>普通模式中，P(大写)代表粘贴至光标前（上）</p>
<h2 id="剪切及粘贴"><a href="#剪切及粘贴" class="headerlink" title="剪切及粘贴"></a>剪切及粘贴</h2><p>dd删除命令其实是剪切，每次dd删除文档内容后，使用p来粘贴，这样可以实现—-交换上下行</p>
<h1 id="查找替换"><a href="#查找替换" class="headerlink" title="查找替换"></a>查找替换</h1><h2 id="字符的替换及撤销操作"><a href="#字符的替换及撤销操作" class="headerlink" title="字符的替换及撤销操作"></a>字符的替换及撤销操作</h2><table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>r+&lt;待替换字母&gt;</td>
<td>将游标所在字母替换为指定字母</td>
</tr>
<tr>
<td>R</td>
<td>连续替换，直到按下Esc</td>
</tr>
<tr>
<td>cc</td>
<td>替换整行，即删除游标所在行，并进入插入模式</td>
</tr>
<tr>
<td>cw</td>
<td>替换一个单词，即删除一个单词，并进入插入模式</td>
</tr>
<tr>
<td>C(大写)</td>
<td>替换游标以后至行末</td>
</tr>
<tr>
<td>~</td>
<td>反转游标所在字母大小写</td>
</tr>
<tr>
<td>u{n}</td>
<td>撤销一次或n次操作</td>
</tr>
<tr>
<td>U(大写)</td>
<td>撤销当前行的所有修改</td>
</tr>
<tr>
<td>Ctrl+r</td>
<td>redo，即撤销undo的操作</td>
</tr>
</tbody>
</table>
<h2 id="快速缩进"><a href="#快速缩进" class="headerlink" title="快速缩进"></a>快速缩进</h2><p>普通模式下操作 </p>
<pre><code>&gt;&gt; 整行向右缩进
&lt;&lt; 整行向左回退
</code></pre><p>普通模式下，对shftwidth值进行设置，可以控制缩进和回退e字符数</p>
<pre><code>:set shiftwidth? //查看当前缩进字符数
:set shiftwidth=10 //设置缩进字符数为10
</code></pre><p>shiftwidthn可以简写为sw</p>
<h2 id="调整文本位置"><a href="#调整文本位置" class="headerlink" title="调整文本位置"></a>调整文本位置</h2><p>命令行模式下，操作光标所在的行</p>
<pre><code>:ce //本行内容剧中
:ri //本行内容靠右
:le //本行内容靠左
</code></pre><h2 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h2><p>普通模式下，输入 <em>/</em>, 然后输入需要查找的字符串，按回车即可进行查找。 <em>? </em>功能与 <em>/</em> 功能相同，只不过 <em>?</em>向下查找。进入查找之后，在字符串之后输入<em>n</em>和<em>N</em> 可以继续查找，<em>n</em>表示继续查找，<em>N</em>表示方向查找</p>
<h2 id="高级查找"><a href="#高级查找" class="headerlink" title="高级查找"></a>高级查找</h2><p>普通模式下输入*寻找游标所在处的单词<br><br>普通模式下输入#同上，但 # 是向前（上）找，*则是向后（下）找<br><br>普通模式下输入g*同* ，但部分符合该单词即可<br><br>普通模式下输入g#同# ，但部分符合该单词即可<br></p>
<h1 id="高级功能"><a href="#高级功能" class="headerlink" title="高级功能"></a>高级功能</h1><h2 id="多文件编辑"><a href="#多文件编辑" class="headerlink" title="多文件编辑"></a>多文件编辑</h2><h3 id="vim编辑多个文件"><a href="#vim编辑多个文件" class="headerlink" title="vim编辑多个文件"></a>vim编辑多个文件</h3><pre><code>vim 1.txt 2.txt
</code></pre><p>默认进入<em>1.txt</em>文件的编辑界面<br></p>
<ul>
<li>命令行模式下输入<em> :n </em>编辑 <em>2.txt </em>文件，可以加<em> ! </em>即<em> :n! </em>强制切换，之前一个文件的输入没有保存，仅仅切换到另一个文件<br></li>
</ul>
<ul>
<li>命令行模式下输入<em> :N </em>编辑 <em>1.txt </em>文件，可以加<br><em>! </em>即 <em>:N!</em> 强制切换，之前文件内的输入没有保存，仅仅是切换到另一个文件</li>
</ul>
<h3 id="进入vim后打开新文件"><a href="#进入vim后打开新文件" class="headerlink" title="进入vim后打开新文件"></a>进入vim后打开新文件</h3><ul>
<li>命令行模式下输入<code>:e 3.txt</code> 打开新文件<code>3.txt</code></li>
<li>命令行模式下输入<code>:e#</code>回到前一个文件</li>
<li>命令行模式下输入<code>:ls</code>可以列出以前编辑过的文档</li>
<li>命令行模式下输入<code>:b 2.txt</code>（或者编号）可以直接进入文件2.txt编辑</li>
<li>命令行模式下输入<code>:bd 2.txt</code>（或者编号）可以删除以前编辑过的列表中的文件项目</li>
<li>命令行模式下输入<code>:e! 4.txt</code>，新打开文件<code>4.txt</code>，放弃正在编辑的文件</li>
<li>命令行模式下输入<code>:f</code> 显示正在编辑的文件名</li>
<li>命令行模式下输入<code>:f new.txt</code>，改变正在编辑的文件名字为<code>new.txt</code></li>
</ul>
<h3 id="恢复文件"><a href="#恢复文件" class="headerlink" title="恢复文件"></a>恢复文件</h3><p>如果因为断电等原因造成文档没有保存，可以采用恢复方式，<code>vim -r</code>进入文档后，输入<code>:ewcover 1.txt</code>来恢复</p>
<pre><code>vim -r 1.txt
</code></pre><h2 id="可视模式"><a href="#可视模式" class="headerlink" title="可视模式"></a>可视模式</h2><h3 id="可视模式命令简介"><a href="#可视模式命令简介" class="headerlink" title="可视模式命令简介"></a>可视模式命令简介</h3><ul>
<li>在普通模式下输入 v（小写），进入字符选择模式，就可以移动光标，光标走过的地方就会选取。再次按下v会后就会取消选取。</li>
<li>在普通模式下输入 Shift+v（小写），进入行选择模式，按下V之后就会把整行选取，您可以上下移动光标选更多的行，同样，再按一次 Shift+v 就可以取消选取。</li>
<li>在普通模式下输入 Ctrl+v（小写），这是区域选择模式，可以进行矩形区域选择，再按一次 Ctrl+v 取消选取。</li>
<li>在可视模式下输入 d 删除选取区域内容<br>在可视模式下输入y复制选取区域内容<h3 id="可视模式命令练习"><a href="#可视模式命令练习" class="headerlink" title="可视模式命令练习"></a>可视模式命令练习</h3></li>
<li>在普通模式下9G跳转到第9行,输入Shift+v（小写V），进入可视模式进行行选择，选中5行，按下&gt;&gt;缩进，将5行整体缩进一个shiftwidth</li>
<li>在普通模式下输入 Ctrl+v（小写V），进入可视模式进行矩形区域选择，选中第一列字符然后x删除整列</li>
</ul>
<h2 id="视窗操作"><a href="#视窗操作" class="headerlink" title="视窗操作"></a>视窗操作</h2><p>vim 可以在一个界面里打开多个窗口进行编辑，这些编辑窗口称为 vim 的视窗。 打开方法有很多种，例如可以使用在命令行模式下输入 :new 打开一个新的 vim 视窗，并进入视窗编辑一个新文件（普通模式下输入 Ctrl+w也可以），除了 :new 命令，下述列举的多种方法也可以在命令模式或普通模式下打开新的视窗：</p>
<p><strong>注意：快捷键可能会与浏览器的快捷键冲突，可换为 IE 浏览器进行实验或者在浏览器设置里禁用浏览器快捷键。
</strong></p>
<ul>
<li>命令行模式下输入:sp 1.txt 打开新的水平分屏视窗来编辑1.txt</li>
<li>命令行模式下输入:vsp 2.txt 打开新的垂直分屏视窗来编辑2.txt</li>
<li>普通模式下Ctrl+w s 将当前窗口分割成两个水平的窗口</li>
<li>普通模式下Ctrl+w v 将当前窗口分割成两个垂直的窗口</li>
<li>普通模式下Ctrl+w q 即 :q 结束分割出来的视窗。如果在新视窗中有输入需要使用强制符！即:q!</li>
<li>普通模式下Ctrl+w o 打开一个视窗并且隐藏之前的所有视窗</li>
<li>普通模式下Ctrl+w j 移至下面视窗</li>
<li>普通模式下Ctrl+w k 移至上面视窗</li>
<li>普通模式下Ctrl+w h 移至左边视窗</li>
<li>普通模式下Ctrl+w l 移至右边视窗</li>
<li>普通模式下Ctrl+w J 将当前视窗移至下面</li>
<li>普通模式下Ctrl+w K 将当前视窗移至上面</li>
<li>普通模式下Ctrl+w H 将当前视窗移至左边</li>
<li>普通模式下Ctrl+w L 将当前视窗移至右边</li>
<li>普通模式下Ctrl+w 减小视窗的高度</li>
<li>普通模式下Ctrl+w + 增加视窗的高度</li>
</ul>
<h2 id="创建加密文档"><a href="#创建加密文档" class="headerlink" title="创建加密文档"></a>创建加密文档</h2><pre><code>vim -x nginx.conf
</code></pre><p>根据提示输入密码，下次打开文件时，需要输入密码</p>
<h2 id="vim执行外部命令"><a href="#vim执行外部命令" class="headerlink" title="vim执行外部命令"></a>vim执行外部命令</h2><p>在命令行模式中输入!可以执行外部的shell命令</p>
<ul>
<li><code>:!ls</code> 用于显示当前目录的内容</li>
<li><code>:!rm FILENAME</code>用于删除名为 FILENAME 的文件</li>
<li><code>:w FILENAME</code>可将当前 VIM 中正在编辑的文件另存为 FILENAME 文件</li>
</ul>
<h2 id="vim中查看帮助"><a href="#vim中查看帮助" class="headerlink" title="vim中查看帮助"></a>vim中查看帮助</h2><ul>
<li>普通模式下按F1打开vim自己预设的帮助文档</li>
<li>命令行模式下输入:h shiftwidth 打开名为shiftwidth的帮助文件</li>
<li>命令行模式下输入:ver 显示版本及参数</li>
</ul>
<h2 id="功能设定"><a href="#功能设定" class="headerlink" title="功能设定"></a>功能设定</h2><h3 id="vim的功能设定"><a href="#vim的功能设定" class="headerlink" title="vim的功能设定"></a>vim的功能设定</h3><p>可以在编辑文件的时候进行功能设定，如命令行模式下输入:set nu（显示行数），设定值退出vim后不会保存。要永久保存配置需要修改vim配置文件。 vim的配置文件~/.vimrc(实验楼环境中配置文件在/etc/vim/vimrc)，可以打开文件进行修改，不过务必小心不要影响vim正常使用</p>
<h3 id="获取目前的设定"><a href="#获取目前的设定" class="headerlink" title="获取目前的设定"></a>获取目前的设定</h3><ul>
<li>命令行模式下输入:set或者:se显示所有修改过的配置</li>
<li>命令行模式下输入:set all 显示所有的设定值</li>
<li>命令行模式下输入:set option? 显示option的设定值</li>
<li>命令行模式下输入:set nooption 取消当前设定值<h3 id="set功能的说明"><a href="#set功能的说明" class="headerlink" title="set功能的说明"></a>set功能的说明</h3></li>
<li>命令行模式下输入:set autoindent(ai) 设置自动缩进</li>
<li>命令行模式下输入:set autowrite(aw) 设置自动存档，默认未打开</li>
<li>命令行模式下输入:set background=dark或light，设置背景风格</li>
<li>命令行模式下输入:set backup(bk) 设置自动备份，默认未打开</li>
<li>命令行模式下输入: set cindent(cin) 设置C语言风格缩进<br>-<br>以上内容来自<a href="https://www.shiyanlou.com/courses/2" title="实验楼" target="_blank" rel="noopener">https://www.shiyanlou.com/courses/2</a>，为作者边学习，边摘抄和总计的内容</li>
</ul>

      
    </div>
    
    
    

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.jpg" alt="zxp wechat" style="width: 200px; max-width: 100%;"/>
    <div>欢迎关注微信公众号！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/WeChatpay.jpg" alt="zxp 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/Alipay.jpg" alt="zxp 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    zxp
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zxpgo.github.io/2018/08/13/Vim教程/" title="Vim教程">https://zxpgo.github.io/2018/08/13/Vim教程/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tag/Linux/" rel="tag"># Linux</a>
          
            <a href="/tag/教程/" rel="tag"># 教程</a>
          
            <a href="/tag/vim/" rel="tag"># vim</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/12/Markdown教程/" rel="next" title="Markdown教程">
                <i class="fa fa-chevron-left"></i> Markdown教程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/13/VPN安装后无法连接互联网/" rel="prev" title="VPN安装后无法连接到互联网">
                VPN安装后无法连接到互联网 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODgxNC8xNTM0Mg=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.gif"
                alt="zxp" />
            
              <p class="site-author-name" itemprop="name">zxp</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">176</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">48</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_25774883" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-globe"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/zxpgo/zxpgo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.linkedin.com/feed/" target="_blank" title="LinkedIn">
                      
                        <i class="fa fa-fw fa-globe"></i>LinkedIn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="1165772354@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://theme-next.iissnan.com" title="Next主题" target="_blank">Next主题</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://blog.rexking6.top" title="青爷博客" target="_blank">青爷博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.csdn.net/download/qq_25774883" title="CSDN下载" target="_blank">CSDN下载</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.livere.com/" title="来必力" target="_blank">来必力</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://tongji.baidu.com/web/welcome/login" title="百度统计" target="_blank">百度统计</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leancloud.cn/" title="LeanCloud" target="_blank">LeanCloud</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ibruce.info/2015/04/04/busuanzi/" title="不蒜子" target="_blank">不蒜子</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leetcode-cn.com/" title="LeetCode" target="_blank">LeetCode</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式："><span class="nav-number">1.</span> <span class="nav-text">Vim具有6种基本模式和5种派生模式，我们这里只简单介绍下6种基本模式：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#游标移动"><span class="nav-number">2.</span> <span class="nav-text">游标移动</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#从普通模式进入插入模式"><span class="nav-number">3.</span> <span class="nav-text">从普通模式进入插入模式</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#保存文件"><span class="nav-number">4.</span> <span class="nav-text">保存文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#退出vim"><span class="nav-number">5.</span> <span class="nav-text">退出vim</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#普通模式下删除vim文本信息"><span class="nav-number">6.</span> <span class="nav-text">普通模式下删除vim文本信息</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#vim重复命令（普通模式下）"><span class="nav-number">7.</span> <span class="nav-text">vim重复命令（普通模式下）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#行间跳转"><span class="nav-number">7.1.</span> <span class="nav-text">行间跳转.</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#行内跳转"><span class="nav-number">7.2.</span> <span class="nav-text">行内跳转</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#复制粘贴和剪切"><span class="nav-number">8.</span> <span class="nav-text">复制粘贴和剪切</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#复制及粘贴"><span class="nav-number">8.1.</span> <span class="nav-text">复制及粘贴</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#剪切及粘贴"><span class="nav-number">8.2.</span> <span class="nav-text">剪切及粘贴</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查找替换"><span class="nav-number">9.</span> <span class="nav-text">查找替换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#字符的替换及撤销操作"><span class="nav-number">9.1.</span> <span class="nav-text">字符的替换及撤销操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#快速缩进"><span class="nav-number">9.2.</span> <span class="nav-text">快速缩进</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#调整文本位置"><span class="nav-number">9.3.</span> <span class="nav-text">调整文本位置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查找"><span class="nav-number">9.4.</span> <span class="nav-text">查找</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#高级查找"><span class="nav-number">9.5.</span> <span class="nav-text">高级查找</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#高级功能"><span class="nav-number">10.</span> <span class="nav-text">高级功能</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#多文件编辑"><span class="nav-number">10.1.</span> <span class="nav-text">多文件编辑</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vim编辑多个文件"><span class="nav-number">10.1.1.</span> <span class="nav-text">vim编辑多个文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#进入vim后打开新文件"><span class="nav-number">10.1.2.</span> <span class="nav-text">进入vim后打开新文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#恢复文件"><span class="nav-number">10.1.3.</span> <span class="nav-text">恢复文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#可视模式"><span class="nav-number">10.2.</span> <span class="nav-text">可视模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#可视模式命令简介"><span class="nav-number">10.2.1.</span> <span class="nav-text">可视模式命令简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#可视模式命令练习"><span class="nav-number">10.2.2.</span> <span class="nav-text">可视模式命令练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#视窗操作"><span class="nav-number">10.3.</span> <span class="nav-text">视窗操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建加密文档"><span class="nav-number">10.4.</span> <span class="nav-text">创建加密文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vim执行外部命令"><span class="nav-number">10.5.</span> <span class="nav-text">vim执行外部命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vim中查看帮助"><span class="nav-number">10.6.</span> <span class="nav-text">vim中查看帮助</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#功能设定"><span class="nav-number">10.7.</span> <span class="nav-text">功能设定</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vim的功能设定"><span class="nav-number">10.7.1.</span> <span class="nav-text">vim的功能设定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取目前的设定"><span class="nav-number">10.7.2.</span> <span class="nav-text">获取目前的设定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set功能的说明"><span class="nav-number">10.7.3.</span> <span class="nav-text">set功能的说明</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div>
<script async src="https//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv" style='display:none'>
    本站总访问量 <span id="busuanzi_value_site_pv"></span> 
    <span class="post-meta-divider">|</span>
</span>
<span id="busuanzi_container_site_uv" style='display:none'>
    访问人数 <span id="busuanzi_value_site_uv"></span>
</span>
</div>


<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; 2018-8 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-"></i> Power by 
  </span>
  <span class="author" itemprop="copyrightHolder">zxp</span>
  
  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  




  
  <div id="lv-container" data-uid="MTAyMC8zODgxNC8xNTM0Mg==">
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
	</div>
  











  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "top";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.4"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.4"></script>


  
  <script type="text/javascript" src="/js/src/exturl.js?v=5.1.4"></script>


  
  
  	 <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
<script>
function showTime(Counter) {
	var query = new AV.Query(Counter);
	$(".leancloud_visitors").each(function() {
		var url = $(this).attr("id").trim();
		query.equalTo("url", url);
		query.find({
			success: function(results) {
				if (results.length == 0) {
					var content = $(document.getElementById(url)).text() + ' 0';
					$(document.getElementById(url)).text(content);
					return;
				}
				for (var i = 0; i < results.length; i++) {
					var object = results[i];
					var content = $(document.getElementById(url)).text() + ' ' + object.get('time');
					$(document.getElementById(url)).text(content);
				}
			}
		});

	});
}

</script>
  
</body>
</html>
